package com.zp.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.api.entity.SysRoleGroup;
import com.zp.system.entity.SysRoleGroupMenu;
import com.zp.system.entity.bo.SysRoleGroupBo;
import com.zp.system.entity.vo.SysRoleGroupVo;
import com.zp.system.mapper.SysRoleGroupMapper;
import com.zp.system.mapper.SysRoleGroupMenuMapper;
import com.zp.system.service.ISysRoleGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 自定义服务信息Service业务层处理
 *
 * @author zhang peng
 * @date 2022-12-11
 */
@RequiredArgsConstructor
@Service
public class SysRoleGroupServiceImpl implements ISysRoleGroupService {

    private final SysRoleGroupMapper baseMapper;
    private final SysRoleGroupMenuMapper sysRoleGroupMenuMapper;

    /**
     * 查询自定义服务信息
     */
    @Override
    public SysRoleGroupVo queryById (String roleGroupId) {
        return baseMapper.selectVoById(roleGroupId);
    }

    /**
     * 查询自定义服务信息列表
     */
    @Override
    public TableDataInfo<SysRoleGroupVo> queryPageList (SysRoleGroupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysRoleGroup> lqw = buildQueryWrapper(bo);
        Page<SysRoleGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<SysRoleGroup> selectList (SysRoleGroupBo bo) {
        LambdaQueryWrapper<SysRoleGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询自定义服务信息列表
     */
    @Override
    public List<SysRoleGroupVo> queryList (SysRoleGroupBo bo) {
        LambdaQueryWrapper<SysRoleGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysRoleGroup> buildQueryWrapper (SysRoleGroupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysRoleGroup> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getRoleGroupName()), SysRoleGroup::getRoleGroupName, bo.getRoleGroupName());
        lqw.eq(StringUtils.isNotBlank(bo.getRoleKey()), SysRoleGroup::getRoleKey, bo.getRoleKey());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), SysRoleGroup::getOrgId, bo.getOrgId());
        lqw.eq(bo.getRoleSort() != null, SysRoleGroup::getRoleSort, bo.getRoleSort());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysRoleGroup::getStatus, bo.getStatus());
        lqw.orderByAsc(SysRoleGroup::getRoleSort);
        return lqw;
    }

    /**
     * 新增自定义服务信息
     */
    @Override
    public Boolean insertByBo (SysRoleGroupBo bo) {
        SysRoleGroup add = BeanUtil.toBean(bo, SysRoleGroup.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRoleGroupId(add.getRoleGroupId());
            this.insertRoleGroupMenu(bo, add);
        }
        return flag;
    }

    /**
     * 自定义服务信息业务对象 批量新增
     *
     * @param bo
     */
    public void insertRoleGroupMenu (SysRoleGroupBo bo, SysRoleGroup sysRoleGroup) {
        List<SysRoleGroupMenu> list = new ArrayList<>();
        for (String menuId : bo.getMenuIds()) {
            SysRoleGroupMenu sysRoleGroupMenu = new SysRoleGroupMenu();
            sysRoleGroupMenu.setMenuId(menuId);
            sysRoleGroupMenu.setRoleGroupId(sysRoleGroup.getRoleGroupId());
            list.add(sysRoleGroupMenu);
        }
        sysRoleGroupMenuMapper.insertBatch(list);
    }

    /**
     * 修改自定义服务信息
     */
    @Override
    public Boolean updateByBo (SysRoleGroupBo bo) {
        SysRoleGroup update = BeanUtil.toBean(bo, SysRoleGroup.class);
        validEntityBeforeSave(update);
        //删除已选菜单id
        sysRoleGroupMenuMapper.deleteGroupMenu(bo.getRoleGroupId());
        this.insertRoleGroupMenu(bo, update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (SysRoleGroup entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除自定义服务信息
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Map<String, Object> getNotExistsRoleGroup (String roleId, String orgId) {
        Map<String, Object> map = new HashMap<>();
        // 获取未选择的自定义服务列表
        List<SysRoleGroup> list = baseMapper.getNotExistsRoleGroup(roleId, orgId);
        // 获取角色存在的自定义服务列表
        List<SysRoleGroup> haveChooseService = baseMapper.getExistsRoleGroup(roleId, orgId);
        map.put("list" , list);
        map.put("haveChooseService" , haveChooseService);
        return map;
    }

    /**
     * 根据userId获取已选择的自定义服务列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<SysRoleGroup> selectListChooseRoleGroup (String userId, String orgId) {
        return baseMapper.selectListChooseRoleGroup(userId, orgId);
    }
}
